package org.elec.mac.controller;

import lombok.extern.slf4j.Slf4j;
import org.elec.mac.entity.*;
import org.elec.mac.page.PageList;
import org.elec.mac.page.PageProperty;
import org.elec.mac.service.SAttachedService;
import org.elec.mac.service.SMaterialService;
import org.elec.mac.service.SProductService;
import org.elec.mac.service.StatisticsService;
import org.elec.mac.utils.JSONUtil;
import org.elec.mac.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销量
 * Created by Bean on 2018/6/5.
 */
@Slf4j
@Controller
public class StatisticsController extends BaseController {

    @Autowired
    private StatisticsService statisticsService;

    @RequestMapping("/sales_list.do")
    public String saleListHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                     @RequestParam(value = "action", required = false, defaultValue = "") String action,
                                     @RequestParam(value = "limit", required = false) String limit,
                                     @RequestParam(value = "page", required = false) String page) throws IOException {

        if ("page".equals(action)) {
            PageProperty pp = new PageProperty();
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                this.setPageInfo(request.getCookies(), pp, limit, page);
                PageList<Statistics> lintPageList = statisticsService.getPageList(pp);
                map.put("success", true);
                map.put("records", lintPageList.getRecords());
                map.put("recordsTotal", lintPageList.getTotalRecords());
                map.put("recordsFiltered", lintPageList.getTotalRecords());
            } catch (Exception ex) {
                log.error("销量列表获取错误", ex);
                map.put("success", false);
                map.put("msg", "销量列表获取错误");
            }
            response.getWriter().write(JSONUtil.map2json(map));
            return null;
        }
        return "/sales/sales_list";
    }

    @RequestMapping("/sales_bar.do")
    public String saleBarHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                  @RequestParam(value = "startTime", required = false) String startTime,
                                  @RequestParam(value = "endTime", required = false) String endTime,
                                  @RequestParam(value = "action", required = false) String action) throws IOException {

        return getPagePath(startTime, endTime, "bar", model);
    }

    @RequestMapping("/sales_pie.do")
    public String salePieHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                 @RequestParam(value = "startTime", required = false) String startTime,
                                 @RequestParam(value = "endTime", required = false) String endTime,
                                 @RequestParam(value = "action", required = false) String action) throws IOException {

        return getPagePath(startTime, endTime, "pie", model);
    }

    private String getPagePath(String startTime, String endTime, String action, ModelMap model) {
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("msg", "销量数据获取");
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd" );
            if(null == startTime) {
                startTime = sdf.format(new Date());
            }
            if(null == endTime) {
                endTime = sdf.format(new Date());
            }
            map.put("startTime", sdf.parse(startTime));
            map.put("endTime", sdf.parse(endTime));
            List<Map<String, Object>> saleList = statisticsService.getSalePicData(map);
            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            StringBuilder sb3 = new StringBuilder();
            for (int i = 0; i < saleList.size(); i++) {
                sb.append("\""+saleList.get(i).get("machname")+"\"");
                sb1.append("\""+saleList.get(i).get("count")+"\"");
                int r = (int)(Math.random()*255+150);
                int g = (int)(Math.random()*255+100);
                int b = (int)(Math.random()*255+50);
                sb2.append("'rgba("+r+","+g+","+b+",0.2)'");
                sb3.append("'rgba("+r+","+g+","+b+",1)'");
                if(i < saleList.size()-1) {
                    sb.append(",");
                    sb1.append(",");
                    sb2.append(",");
                    sb3.append(",");
                }
            }
            log.info("图表数据=====》"+sb+" 销量数据=====》"+sb1);
            log.info("背景颜色=====》"+sb2+" 边框颜色=====》"+sb3);
            model.put("salesLable", sb.toString());
            model.put("salesData", sb1.toString());
            model.put("backgroundColor", sb2.toString());
            model.put("borderColor", sb3.toString());
            model.put("startTime", startTime);
            model.put("endTime", endTime);
        } catch (Exception ex) {
            log.error("销量数据获取错误", ex);
            map.put("success", false);
            map.put("msg", "销量数据获取错误");
        }
        if("bar".equals(action)) {
            return "sales/sales_bar";
        } else if ("pie".equals(action)) {
            return "sales/sales_pie";
        }
        return null;
    }
}
